extends Control
class_name SkillButton

## 蓝量
@export var need_mp:int
## 基础技能冷却时间
@export var origin_cold_time:float 
## 实际技能冷却时间
@export var current_cold_time:float :
	set(value):
		current_cold_time = value
		if value > 0:
			timer.wait_time = value
			cool_down.max_value = timer.wait_time * 100
## 技能图片
@onready var skill_image: TextureRect = %Image
@onready var timer: Timer = %Timer
## 绑定按键名称
@onready var key: Label = %Key
@onready var time: Label = %Time
## 冷却图标
@onready var cool_down: TextureProgressBar = %coolDown
@onready var button: Button = %Button

## 技能描述container
@onready var panel_container: PanelContainer = %PanelContainer
## 技能名称
@onready var skill_name: Label = %skillName
## 技能描述
@onready var skill_info: Label = %skillInfo


var skill_pandora_entity: PandoraEntity:
	set(value):
		skill_pandora_entity = value
		skill_name.text = skill_pandora_entity.get_string("name")
		var temp_text = "等级:"
		temp_text += str(skill_pandora_entity.get_integer("level")) + "\n"
		if need_mp >= 0:
			temp_text += "冷却时间:" + str(skill_pandora_entity.get_float("cool_down")) + "秒\n"
		if skill_pandora_entity.get_integer("need_mp") < 0:
			temp_text += "被动技能\n"
		else:
			temp_text += "主动技能\n"
		temp_text += "效果:"
		temp_text += skill_pandora_entity.get_string("skill_info")
		skill_info.text = temp_text
var skill_func: Callable
var skill_index: String :
	set(value):
		skill_index = value
		if !UserSettings.key_binding_changed.is_connected(on_key_bingding_changed):
			UserSettings.key_binding_changed.connect(on_key_bingding_changed)
		
func _ready() -> void:
	set_process(false)
	cool_down.value = 0
	key.visible = false
	panel_container.visible = false
	
func _process(_delta: float) -> void:
	time.text = "%3.1f" % timer.time_left
	cool_down.value = timer.time_left * 100
	
#获取技能触发的效果
func apply()->void:
	skill_func.call()

func bind_key(_key_name:String = key.text):
	if _key_name != null and !_key_name.is_empty():
		key.visible = true
		key.text = _key_name
	button.shortcut = Shortcut.new()
	var input_key = InputEventKey.new()
	input_key.keycode = OS.find_keycode_from_string(_key_name)
	button.shortcut.events = [input_key]

## 恢复技能冷却
func reset_cooldown() -> void:
	if timer:
		timer.stop()
	button.disabled = false
	time.text = ""
	cool_down.value = 0
	set_process(false)
	print("技能冷却已恢复: " + skill_name.text)

func _on_timer_timeout() -> void:
	button.disabled = false
	time.text = ""
	cool_down.value = 0
	set_process(false)

## 判断蓝量是否足够，足够才去使用技能
func _on_button_pressed() -> void:
	## 如果需要的蓝小于0，说明是被动技能
	if need_mp < 0:
		print("玩家尝试使用被动技能：" + skill_name.text)
		return
	
	# 特殊处理"燃烧吧"技能
	if skill_name.text == "燃烧吧":
		# 检查是否已经有燃烧buff
		if Utils.player.buff_manager.has_buff("燃烧"):
			# 取消燃烧时不需要消耗蓝量
			self.apply()
			timer.start()
			button.disabled = true
			set_process(true)
			return
		else:
			# 开始燃烧时需要检查蓝量
			if Utils.player.player_stats.get_float("current_mp") < need_mp:
				Utils.show_string_label_c("蓝量不够", Utils.player, Color.BLUE)
				return	
			Utils.set_player_stats_value("current_mp", -need_mp)
			self.apply()
			timer.start()
			button.disabled = true
			set_process(true)
			return
	
	# 其他技能的正常逻辑
	if Utils.player.player_stats.get_float("current_mp") < need_mp:
		Utils.show_string_label_c("蓝量不够", Utils.player, Color.BLUE)
		return	
	Utils.set_player_stats_value("current_mp", -need_mp)	
	self.apply()
	timer.start()
	button.disabled = true
	set_process(true)

func _on_button_mouse_entered() -> void:
	panel_container.visible = true

func _on_button_mouse_exited() -> void:
	panel_container.visible = false

func on_key_bingding_changed(action_name, new_event):
	## new_event为空时可能被覆盖按键
	if action_name == skill_index and new_event != null:
		bind_key(OS.get_keycode_string(new_event.keycode))

